package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.EasyexcelUtils;
import com.ruoyi.system.domain.excel.SysLogininforExcel;
import com.ruoyi.system.service.ISysDictDataService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysLogininfor;
import com.ruoyi.system.service.ISysLogininforService;

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/logininfor")
public class SysLogininforController extends BaseController
{
    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ISysDictDataService dictDataService;

    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo list(SysLogininfor logininfor)
    {
        startPage();
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }


    @Log(title = "导出登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor)
    {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        // 转换为导出实体类
        List<SysLogininforExcel> excelList = list.stream().map(item -> {
            SysLogininforExcel excel = new SysLogininforExcel();
            excel.setInfoId(item.getInfoId());
            excel.setUserName(item.getUserName());

            // 转换状态字段显示文本
            if (StringUtils.isNotEmpty(item.getStatus())) {
                String statusText = dictDataService.selectDictLabel("login_status", item.getStatus());
                excel.setStatus(StringUtils.isNotEmpty(statusText) ? statusText : item.getStatus());
            } else {
                excel.setStatus(item.getStatus());
            }
            excel.setIpaddr(item.getIpaddr());
            excel.setMsg(item.getMsg());
            excel.setAccessTime(item.getAccessTime());
            return excel;
        }).collect(java.util.stream.Collectors.toList());

        // 使用EasyExcel导出
        EasyexcelUtils.exportExcel(response, excelList, "登录日志", SysLogininforExcel.class);
    }


    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "删除登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable(value = "infoIds") Long[] infoIds)
    {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "清空登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        logininforService.cleanLogininfor();
        return success();
    }

    @RequiresPermissions("system:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName)
    {
        redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName);
        return success();
    }

    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysLogininfor logininfor)
    {
        return toAjax(logininforService.insertLogininfor(logininfor));
    }
}
